// 引入 events 模块
var events=require('events');

// 创建 eventEmitter 对象
var eventEmitter=new events.EventEmitter();
//eventEmitter.setMaxListeners(1);

// 创建事件处理程序
var connectHandler=function connected()
{
	console.log('连接成功');
	// 触发 data_received 事件 
	eventEmitter.emit('data_received',100);
}

// 创建事件处理程序
var connectHandler_sec=function connected()
{
	console.log('连接成功2');
	// 触发 data_received 事件 
	eventEmitter.emit('data_received','aabbccdd');
}

// 绑定 connection 事件处理程序
eventEmitter.on('connection',connectHandler);
// 绑定 connection 事件处理程序
eventEmitter.addListener('connection',connectHandler_sec);

// 使用匿名函数绑定 data_received 事件
eventEmitter.on('data_received',function(str){
	console.log('数据接收成功:'+str);
	
});



var ErrorHandler=function ErrorHandler(err)
{
	console.log('ERROR:'+err);
};
eventEmitter.on('error',ErrorHandler);
eventEmitter.emit('error',1);

/* setTimeout(function()
{
eventEmitter.emit('connection');
},5000); */
eventEmitter.emit('connection');
var count=require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(count+' listeners');



eventEmitter.removeListener('connection',connectHandler_sec);
eventEmitter.emit('connection');
var count=require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(count+' listeners');


console.log('finish');